package mediadisplay.viewerarea 
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.text.TextField;
	import mediadisplay.ui.colors.ColorMap;
	/**
	 * ...
	 * @author Anders Figenschow
	 */
	public class EditFrame extends Sprite
	{
		private var _screenText:ScreenText;
		private var firstMouseDown:Point;
		private var activeAngle:int;
		private var ctrlKeyDown:Boolean = false;
		private var angle1:Sprite;
		private var angle2:Sprite;
		private var angle3:Sprite;
		private var angle4:Sprite;
		private var previousX:Number;
		private var previousY:Number;
		private var coordsTF:TextField;
		private var aDown:Boolean = false;
		private var screenElementTargeted:Boolean = false;
		
		public function EditFrame(screenText:ScreenText) 
		{
			_screenText = screenText;
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event = null):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			
			name = "editframe";
			
			stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
			stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
			addEventListener(Event.REMOVED_FROM_STAGE, cleanup);
			addEventListener(Event.ENTER_FRAME, changeCoordinates);
			
			graphics.clear();
			graphics.lineStyle(2, 0xff0000, 1, true);
			graphics.beginFill(0xffffff, 0.2);
			graphics.drawRect( -_screenText.tf.width / 2, 0, _screenText.tf.width, _screenText.tf.height );
			
			buttonMode = true;
			addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
			
			angle1 = stretchCorner(0);
			angle1.x = (-_screenText.tf.width / 2) - 5;
			angle1.y = -5;
			addChild(angle1);
			angle1.buttonMode = true;
			angle1.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
			
			angle2 = stretchCorner(1);
			angle2.x = (_screenText.tf.width / 2) + 5;
			angle2.y = -5;
			addChild(angle2);
			angle2.buttonMode = true;
			
			angle3 = stretchCorner(2);
			addChild(angle3);
			angle3.x = (_screenText.tf.width / 2) + 5;
			angle3.y = _screenText.tf.height + 5;
			angle3.buttonMode = true;
			
			angle4 = stretchCorner(3);
			addChild(angle4);
			angle4.x = (-_screenText.tf.width / 2) - 5;
			angle4.y = _screenText.tf.height + 5;
			angle4.buttonMode = true;
			
			coordsTF = new TextField();
			//addChild(coordsTF);
			coordsTF.width = 80;
			coordsTF.height = 14;
			coordsTF.border = true;
			coordsTF.borderColor = 0xff0000;
			coordsTF.textColor = 0xffffff;
			coordsTF.text = "xxx / xxx";
			coordsTF.mouseEnabled = false;
			coordsTF.background = true;
			coordsTF.backgroundColor = 0x000000;
			coordsTF.x = coordsTF.y = 5;
			
			_screenText._viewerArea._main.soundgraph.colormap.addEventListener(ColorMap.COLOR_CHOSEN, colorChosen);
		}
		
		private function colorChosen(e:Event):void 
		{
			var currentElement:ScreenText = _screenText._viewerArea.getActiveObject();
			if (_screenText != currentElement) return;
			_screenText.setColor(_screenText._viewerArea._main.soundgraph.colormap.chosenColor);
			trace("color set to " + _screenText._viewerArea._main.soundgraph.colormap.chosenColor);
		}
		
		private function cleanup(e:Event):void 
		{
			removeEventListener(Event.REMOVED_FROM_STAGE, cleanup);
			stage.removeEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
			stage.removeEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
			removeEventListener(Event.ENTER_FRAME, changeCoordinates);
			angle1.removeEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
			_screenText._viewerArea._main.soundgraph.colormap.removeEventListener(ColorMap.COLOR_CHOSEN, colorChosen);
		}
		public function changeCoordinates(e:Event):void {
			coordsTF.text = String(int(_screenText.x)) + " / " + String(int(_screenText.y));
			
			// if 'ctrl' key down
			if (aDown) {
				if (_screenText._viewerArea.mouseIsDown && screenElementTargeted) {
					// if 'ctrl' is held down, and mouse down on actual screen element: rotate element
					if (previousX > _screenText._viewerArea.mouseX) {
						_screenText.rotationY += 2;
					} else if (previousX < _screenText._viewerArea.mouseX) {
						_screenText.rotationY -= 2;
					}
					if (previousY > _screenText._viewerArea.mouseY) {
						_screenText.rotationX -= 2;
					} else if (previousY < _screenText._viewerArea.mouseY) {
						_screenText.rotationX += 2;
					}
				} else {
					// if 'ctrl' is held down, mouse not down: rotate world
					if (previousX > _screenText._viewerArea.mouseX) {
						_screenText._viewerArea.centerClip.rotationY += 1;
					} else if (previousX < _screenText._viewerArea.mouseX) {
						_screenText._viewerArea.centerClip.rotationY -= 1;
					}
					if (previousY > _screenText._viewerArea.mouseY) {
						_screenText._viewerArea.centerClip.rotationX -= 1;
					} else if (previousY < _screenText._viewerArea.mouseY) {
						_screenText._viewerArea.centerClip.rotationX += 1;
					}
					
				}
			} 
			previousX = _screenText._viewerArea.mouseX;
			previousY = _screenText._viewerArea.mouseY;
			
		}
		private function keyDownHandler(e:KeyboardEvent):void 
		{
			if (e.ctrlKey) ctrlKeyDown = true;
			//if (e.charCode == 97) {
			
			return;
			if (e.shiftKey) {
				// 'a'
				aDown = true;
			}
			
			if (_screenText._viewerArea._main.soundgraph.timelineContent._currentFocusedMarker == null) return;
			var currentIndex:int = _screenText._viewerArea._main.soundgraph.timelineContent._currentFocusedMarker.index;
			if (isNaN(currentIndex)) return;
			var currentElement:ScreenText = _screenText._viewerArea.objectsOnScreen[currentIndex];
			if (currentElement == null) return;
			
			if (e.keyCode == 37) {
				// arrow left
				currentElement.x -= 2;
			}
			if (e.keyCode == 39) {
				// arrow right
				currentElement.x += 2;
			}
			if (e.keyCode == 38) {
				// arrow up
				currentElement.y -= 2;
			}
			if (e.keyCode == 40) {
				// arrow down
				currentElement.y += 2;
			}
		}
		
		private function keyUpHandler(e:KeyboardEvent):void 
		{
			ctrlKeyDown = false;
			aDown = false;
		}
		
		private function mouseDownHandler(e:MouseEvent):void 
		{
			if (e.target.name != "corner") {
				// move item
				screenElementTargeted = true;
				firstMouseDown = new Point(stage.mouseX, stage.mouseY);
				stage.addEventListener(MouseEvent.MOUSE_MOVE, moveElementHandler);
				stage.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
				
			} else {
				// resize item
				firstMouseDown = new Point(stage.mouseX, stage.mouseY);
				screenElementTargeted = false;
				
				if (e.target is Sprite) {
					if (int(e.target.rotation) == 0) {
						activeAngle = 0;
					} else if (int(e.target.rotation) == 90) {
						activeAngle = 1;
					} else if (int(e.target.rotation) == 180) {
						activeAngle = 2;
					} else {
						activeAngle = 3;
					}
					
					stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
					stage.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
				}
				
			}
		}
		
		private function moveElementHandler(e:MouseEvent):void 
		{
			ScreenText.HEY_IM_WORKING_HERE = true;
			
			if (ctrlKeyDown) {
				rotateElementHandler(e);
				return;
			}
			
			_screenText.x = -_screenText._viewerArea.objectsContainer.x + stage.mouseX - 512;
			_screenText.y = -_screenText._viewerArea.objectsContainer.y + stage.mouseY - 200;
			
		}
		
		private function rotateElementHandler(e:MouseEvent):void {
			if (previousX < stage.mouseX) _screenText.rotation += 1;
			if (previousX > stage.mouseX) _screenText.rotation -= 1;
			if (previousY < stage.mouseY) _screenText.rotationY -= 1;
			if (previousY > stage.mouseY) _screenText.rotationY += 1;
			previousX = stage.mouseX;
			previousY = stage.mouseY;
			_screenText._rotX = _screenText.rotationX;
			_screenText._rotY = _screenText.rotationY;
			_screenText._rotZ = _screenText.rotationZ;
		}
		
		private function mouseUpHandler(e:MouseEvent):void 
		{
			stage.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
			stage.removeEventListener(MouseEvent.MOUSE_MOVE, moveElementHandler);
			_screenText.w = _screenText.tf.width;
			_screenText.h = _screenText.tf.height;
			ScreenText.HEY_IM_WORKING_HERE = false;
		}
		
		private function mouseMoveHandler(e:MouseEvent):void 
		{
			ScreenText.HEY_IM_WORKING_HERE = true;			
			_screenText.resize(firstMouseDown, activeAngle);
		}
		public function redraw():void {
			graphics.clear();
			graphics.lineStyle(2, 0xff0000, 1, true);
			graphics.beginFill(0xffffff, 0.2);
			graphics.drawRect( -_screenText.tf.width / 2, 0, _screenText.tf.width, _screenText.tf.height );
			
			//if (!angle1) return;
			angle1.x = (-_screenText.tf.width / 2) - 5;
			angle1.y = -5;
			angle2.x = (_screenText.tf.width / 2) + 5;
			angle2.y = -5;
			angle3.x = (_screenText.tf.width / 2) + 5;
			angle3.y = _screenText.tf.height + 5;
			angle4.x = (-_screenText.tf.width / 2) - 5;
			angle4.y = _screenText.tf.height + 5;
			
			y = -_screenText.tf.height / 2;
		}
		private function stretchCorner(angle:int):Sprite {
			var sp:Sprite = new Sprite;
			sp.graphics.lineStyle(2, 0xffffff, 1, true);
			sp.graphics.beginFill(0xff0000, 0);
			sp.graphics.lineTo(5, 0);
			sp.graphics.lineStyle(2, 0xffffff, 0, true);
			sp.graphics.lineTo(5, 5);
			sp.graphics.lineTo(0, 5);
			sp.graphics.lineStyle(2, 0xffffff, 1, true);
			sp.graphics.lineTo(0, 0);
			sp.graphics.endFill();
			sp.name = "corner";
			if (angle == 1) sp.rotation = 90;
			if (angle == 2) sp.rotation = 180;
			if (angle == 3) sp.rotation = 270;
			return sp;
		}
		
	}

}